#include "Bullet.h"
#include "MainGameScreen.h"

Bullet::Bullet(D3DXVECTOR2 pos):GameObject(pos,EObjectType::EBullet)
{
	m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Bullet_ID),10, 1, 10, 0, 9);
	m_state=EState::Alive;
	m_effect = SpriteEffect::None;
	m_direction=EDirection::Left;
	m_pSprite->m_Start=0;
	m_pSprite->m_End=3;
	m_pSprite->m_TimeAni = 0.05f;
	_timeToLive=0;
	_timeToExplosive = 0;
	_timeDraw=0;
}

Bullet::~Bullet()
{
	GameObject::~GameObject();
}

void Bullet::Update(float dt)
{	
	// neu nhu cap nhat thi vien dan se bi no o phia trong cua vat va cham
	// bo qua lan cap nhat nay
	if(m_state == EState::Die)
		return;
	m_pSprite->Update(dt);	
	_timeToLive+=dt;
	if(_timeToLive> TIMELIFE)
		m_state=EState::Die;
	if(m_state==EState::Breaking)
	{
		_timeToLive += dt;
		if(m_pSprite->m_Index<7)
			m_pSprite->SetIndex(7);
		m_pSprite->m_Start = 7;
		m_pSprite->m_End = 9;
		m_pSprite->m_TimeAni = 0.09f;
	}
	else
	{
		m_position += m_velocity*dt*60;
		m_velocity.y -= dt*60;		
	}
}

void Bullet::Draw()
{
	if(m_state!=EState::Die)
		GameObject::Draw();	
}

void Bullet::Boom()
{
	if(m_state!=Breaking && m_state!=Die)
		m_state=Breaking;
}

void Bullet::UpdateCollision(list<GameObject*> objects, float dt)
{
	float timeTmp = 0;
	float moveX,moveY;	
	float normalx,normaly;
	float timeCols = 0;
	Box tmpBox = this->GetBox();	
	for(list<GameObject*>::iterator it=objects.begin(); it!=objects.end(); ++it)
	{
		GameObject* otherObject = (*it);		
		EObjectType typeOther=otherObject->GetObjectType();
		Box otherBox = otherObject->GetBox();		
		if(AABB(tmpBox,otherBox,moveX,moveY) == false)
		{
			if(AABBCheck(GetSweptBroadphaseBox(tmpBox),otherBox)==true || AABBCheck(GetSweptBroadphaseBox(otherBox),tmpBox)==true)
			{
				if(otherObject->GetKindOfObject()==EDynamic)
				{
					tmpBox.vx -= otherBox.vx;
					tmpBox.vy -= otherBox.vy;
					otherBox.vx = 0;
					otherBox.vy = 0;
				}
				else
				{
					tmpBox = this->GetBox();
				}
				timeCols = SweptAABB(tmpBox,otherBox,normalx,normaly);
				if(timeCols > 0 && timeCols < 1)
				{
					timeCols*=dt;
					ECollisionDirect collDriect = GetCollisionDirect(normalx,normaly);
					float remainingtime = dt - timeCols;

					//////////////////////////////---Land---//////////////////////////////////

					if(otherObject->GetKindOfObject()==ESolid)
					{
						switch(collDriect)
						{
						case Colls_Bot:
							{
								m_position.y += m_velocity.y*timeCols*60;
								m_velocity.y = MAXVY;
							}
							break;
						case Colls_Top:
							{
								m_position.y += m_velocity.y*timeCols*60;
								m_velocity.y = -m_velocity.y;
							}
							break;
						case Colls_Left:
							{
								m_position.x += m_velocity.x*timeCols*60 - 1;
								this->Boom();
							}
							break;
						case Colls_Right:
							{
								m_position.x += m_velocity.x*timeCols*60 + 1;
								this->Boom();
							}
							break;
						}
					}			

					//////////////////////////////---Enemy---//////////////////////////////////

					if(typeOther == EFungi || typeOther == ETurtle)
					{
						SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
						otherObject->SetObjectState(KickUp);
						this->Boom();		
						MainGameScreen::score+=100;
					}		

					if(typeOther == EFlowerMonster)
					{
						otherObject->SetObjectState(EState::Die);
						this->Boom();
						MainGameScreen::score+=100;
					}
				}	
			}
		}
		else 
		{		

			if(otherObject->GetKindOfObject()==ESolid)
			{
				if(moveX!=0)
				{
					this->Boom();
				}
				if(moveY>0)
				{
					m_position.y += moveY + 1;
					m_velocity.y = MAXVY;
				}
				if(moveY<0)
				{
					m_position.y += moveY -1;
					m_velocity.y = -MAXVY;
				}
			}
		}
	}
}